package com.venus.finance.transferaccount.service.impl;

import java.util.List;

import com.venus.common.service.impl.BaseServiceImpl;
import com.venus.common.utils.ConditionUtil;
import com.venus.common.utils.DateUtil;
import com.venus.common.utils.MathUtil;
import com.venus.component.page.PageInfo;
import com.venus.finance.transferaccount.dao.ITransferAccountDAO;
import com.venus.finance.transferaccount.form.TransferAccountForm;
import com.venus.finance.transferaccount.service.ITransferAccountService;
import com.venus.model.AccountVO;
import com.venus.model.TransferAccountVO;

public class TransferAccountService extends BaseServiceImpl implements ITransferAccountService {

	private ITransferAccountDAO transferAccountDAO ;

	public void setTransferAccountDAO(ITransferAccountDAO transferAccountDAO) {this.transferAccountDAO = transferAccountDAO;}

	@SuppressWarnings("unchecked")
	@Override
	public List<TransferAccountVO> findAllTransferAccountVOs(List<ConditionUtil> lstConditionUtils) {
		return this.transferAccountDAO.exeHql("from TransferAccountVO where 1=1",lstConditionUtils);
	}
	
	@Override
	public PageInfo getPageInfo(int iPageRecord, int iCurPage, List<ConditionUtil> lstConditionUtils){
		String strCountHql = "select count(TransferAccountVO.transferAccountId) from TransferAccountVO as TransferAccountVO where 1=1";
		String strRecordHql = "from TransferAccountVO where 1=1";
		return this.transferAccountDAO.getPageInfo(strCountHql, strRecordHql, lstConditionUtils, iPageRecord, iCurPage);
	}
	
	@Override
	public void saveTransferAccount(TransferAccountVO objTransferAccountVO) {
		AccountVO fromAccountVO = objTransferAccountVO.getFromAccount();
		AccountVO toAccountVO = objTransferAccountVO.getToAccount();
		float fAmount = objTransferAccountVO.getAmount();
		float fHandFee = objTransferAccountVO.getHandFee();
		// 转出账户 - 转账金额
		fromAccountVO.setAmount(MathUtil.round(fromAccountVO.getAmount() - fAmount - fHandFee, 2).floatValue());
		// 转入账户 + 转账金额
		toAccountVO.setAmount(MathUtil.round(toAccountVO.getAmount() + fAmount, 2).floatValue());
		// 新增转账数据
		this.transferAccountDAO.saveOrUpdateModel(objTransferAccountVO);
	}
	
	@Override
	public void updateTransferAccount(TransferAccountForm transferAccountForm) {
		float fNewAmount = transferAccountForm.getAmount();
		float fNewHandFee = transferAccountForm.getHandFee();
		float fOldAmount = transferAccountForm.getOldAmount();
		float fOldHandFee = transferAccountForm.getOldHandFee();
		TransferAccountVO objTransferAccountVO = this.findOneTransferAccountVO(transferAccountForm.getTransferAccountId());
		// 处理老账户: 将发生的费用复原
		AccountVO objOldFromAccountVO = objTransferAccountVO.getFromAccount();
		AccountVO objOldToAccountVO = objTransferAccountVO.getToAccount();
		// 转出账户 + 旧金额 + 旧手续费
		objOldFromAccountVO.addAmount(fOldAmount);
		objOldFromAccountVO.addAmount(fOldHandFee);
		// 转入账户 - 旧金额
		objOldToAccountVO.subAmount(fOldAmount);
		// 更新原始转出账户
		this.transferAccountDAO.saveOrUpdateModel(objOldFromAccountVO);
		// 更新原始转入账户
		this.transferAccountDAO.saveOrUpdateModel(objOldToAccountVO);
		// 处理新选择的账户
		
		// 选择的新账户
		AccountVO objNewFromAccountVO = this.transferAccountDAO.loadModel(AccountVO.class, transferAccountForm.getFromAccountId());
		AccountVO objNewToAccountVO = this.transferAccountDAO.loadModel(AccountVO.class, transferAccountForm.getToAccountId());
		// 转出账户 - 新金额 - 新手续费
		objNewFromAccountVO.setAmount(MathUtil.round(objNewFromAccountVO.getAmount() - fNewAmount - fNewHandFee, 2).floatValue());
		// 转入账户 + 新金额
		objNewToAccountVO.addAmount(fNewAmount);
		// 更新转账数据
		objTransferAccountVO.setFromAccount(objNewFromAccountVO);
		objTransferAccountVO.setToAccount(objNewToAccountVO);
		objTransferAccountVO.setAmount(transferAccountForm.getAmount());
		objTransferAccountVO.setHandFee(transferAccountForm.getHandFee());
		objTransferAccountVO.setTransferDate(DateUtil.parseDate(transferAccountForm.getTransferDate(), "yyyy-MM-dd"));
		objTransferAccountVO.setNote(transferAccountForm.getNote());
		this.transferAccountDAO.saveOrUpdateModel(objTransferAccountVO);
	}
	
	@Override
	public void deleteTransferAccount(String[] strTransferAccountIds) {
		for(String strTransferAccountId : strTransferAccountIds){
			TransferAccountVO objTransferAccountVO = this.transferAccountDAO.getModel(TransferAccountVO.class, Integer.parseInt(strTransferAccountId));
			AccountVO fromAccountVO = objTransferAccountVO.getFromAccount();
			AccountVO toAccountVO = objTransferAccountVO.getToAccount();
			float fAmount = objTransferAccountVO.getAmount();
			float fHandFee = objTransferAccountVO.getHandFee();
			// 转出账户 + 转账金额 + 手续费
			fromAccountVO.setAmount(MathUtil.round(fromAccountVO.getAmount() + fAmount + fHandFee, 2).floatValue());
			// 转入账户减去转账金额
			toAccountVO.setAmount(MathUtil.round(toAccountVO.getAmount() - fAmount, 2).floatValue());
			// 删除转账数据
			this.transferAccountDAO.deleteModel(objTransferAccountVO);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> groupYearGaveContributeByUser(List<ConditionUtil> lstConditionUtils) {
		return this.transferAccountDAO.exeHql("select DATE_FORMAT(TransferAccountVO.transferDate,'%Y-%m'),TransferAccountVO.pubUser.name,SUM(TransferAccountVO.amount) from TransferAccountVO as TransferAccountVO where 1=1",lstConditionUtils);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> groupYearLostContributeByUser(List<ConditionUtil> lstConditionUtils) {
		return this.transferAccountDAO.exeHql("select DATE_FORMAT(TransferAccountVO.transferDate,'%Y-%m'),TransferAccountVO.toAccount.pubUser.name,SUM(TransferAccountVO.amount) from TransferAccountVO as TransferAccountVO where 1=1",lstConditionUtils);
	}
	
	@Override
	public void deleteTransferAccountVO(TransferAccountVO objTransferAccountVO) {
		this.transferAccountDAO.deleteModel(objTransferAccountVO);
	}

	@Override
	public void saveOrUpdateTransferAccountVO(TransferAccountVO objTransferAccountVO) {
		this.transferAccountDAO.saveOrUpdateModel(objTransferAccountVO);
	}

	@Override
	public TransferAccountVO findOneTransferAccountVO(int iTransferAccountId) {
		return this.transferAccountDAO.getModel(TransferAccountVO.class, iTransferAccountId);
	}
	
	
	
}
